package com.example.lemonservice.module.device.controller;
import com.example.lemoncommon.annotation.GetPermitNoAuth;
import com.example.lemoncommon.entity.Result;
import com.example.lemoncommon.enums.ErrorEnum;
import com.example.lemoncommon.exception.ServiceException;
import com.example.lemonservice.entitiy.Device;
import com.example.lemonservice.entitiy.dto.UpdateDeviceDataDTO;
import com.example.lemonservice.module.device.service.DeviceServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Api(tags = "API-device", value = "设备")
@RestController
public class DeviceController {
    private final DeviceServiceImpl deviceService;

    public DeviceController(DeviceServiceImpl deviceService) {
        this.deviceService = deviceService;
    }
    @GetPermitNoAuth
    @GetMapping("/device/getAllDevice")
    public Result<List<Device>> getAllDevice() {
        return Result.SUCCESS(deviceService.getAllDevice());
    }

    @GetPermitNoAuth(requestMethod = "PUT")
    @RequestMapping(value = "/updateDeviceData", method = RequestMethod.PUT)
    @ApiOperation(value = "更新设备数据", notes = "更新设备数据")
    public Result<Boolean> updateDeviceData(@Valid @RequestBody UpdateDeviceDataDTO dto) {
        if (Objects.isNull(dto)) {
            throw new ServiceException(ErrorEnum.PARAM_ERROR.getErrCode(), ErrorEnum.PARAM_ERROR.getErrMsg());
        }
        return Result.SUCCESS(deviceService.updateDeviceData(dto));
    }
}
